package com.example.springboot_manage.controller;


import com.alibaba.druid.util.StringUtils;
import com.example.springboot_manage.model.Account;

import com.example.springboot_manage.model.Admin;
import com.example.springboot_manage.service.AccountService;
import com.example.springboot_manage.service.CookieService;
import com.example.springboot_manage.util.ResultUtil;
import com.example.springboot_manage.vo.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@Controller
@RequestMapping("/user")
public class UserLoginHandler {

    @Autowired
    private AccountService accountService;
    @Autowired
    private CookieService cookieService;

    @Autowired
    private JavaMailSender mailSender;

    @Value("${spring.mail.username}")
    private String Sender; //读取配置文件中的参数



    @RequestMapping("/login")
    @ResponseBody
    public ResultVO loginPost(Admin admin, HttpServletRequest request, HttpServletResponse response){
        String ticket = accountService.login(admin);
        if (!StringUtils.isEmpty(ticket)){
            boolean result = cookieService.setCookie(request,response,ticket);
            request.getSession().setAttribute("user",admin);
            int id = accountService.findAdminId(admin);
            if (id!=0){
                Account account = accountService.findLoginInfo(id,ticket);
                request.getSession().setAttribute("loginInfo",account);
            }
            return result ? ResultUtil.success():ResultUtil.error();
        }
        return ResultUtil.error();

    }


    @GetMapping("/forget")
    public String forget(){
        return "forget";
    }

    @PostMapping("/forget.do")
    public String forgetPost(@RequestParam("username") String userName,@RequestParam("email") String email, Model model) {
        Account account = accountService.findByUsername(userName);
        if (account == null){
            model.addAttribute("error", "帐号不存在或邮箱不正确！");
        }
        else if (account.getEmail().equals(email)){
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(Sender);//发送的邮箱
            message.setTo(email);   //接收者邮箱
            message.setSubject("XX商城后台管理系统-密码找回");
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(account.getUsername() + "用户您好！您的注册密码是：" + account.getPassword() + "。感谢您使用我们的商城管理系统！");
            message.setText(stringBuilder.toString());
            mailSender.send(message);
            model.addAttribute("msg", "密码已发到您的邮箱,请查收！");
        }else{
            model.addAttribute("error", "帐号不存在或邮箱不正确！");
        }
        return "forget";
    }

    @GetMapping("/error")
    public String error(){
        return "error";
    }

    @GetMapping("/loginOut")
    public String loginOut(HttpServletRequest request,HttpServletResponse response) {
        String ticket  = cookieService.getCookie(request);
        accountService.logout(ticket);
        request.getSession().removeAttribute("user");
        request.getSession().removeAttribute("loginInfo");
        cookieService.deleteCookie(request,response);
        return "login";
    }

    @RequestMapping("/home")
     public String toHome(){
        return "home";
    }

    @RequestMapping("/mhome")
    public String toMhome(){
        return "mhome";
    }

    @RequestMapping("/merHome")
    public String toMerHome(){
        return "merHome";
    }

}
